package cate.game.role.pvp.championrace.vo;

import cate.common.table.d.GDChampionRace;
import cate.common.table.d.GDFight;
import cate.game.pvp.championrace.data.ChampionRaceBetRecord;
import cate.game.pvp.championrace.data.ChampionRaceSpec;
import cate.game.res.MixRes;
import cate.game.role.RoleSnapshot;
import easy.java.dev.note.NoteField;
import lombok.Data;

@Data
public class ChampionRaceBetVO {
    private ChampionRaceSpec spec;

    @NoteField(value = "A方快照")
    private RoleSnapshot snapshotA;

    @NoteField(value = "B方快照")
    private RoleSnapshot snapshotB;

    @NoteField(value = "竞猜银币")
    private long coins;

    @NoteField(value = "获得银币")
    public long getCoins;

    @NoteField(value = "竞猜支持")
    private byte side;

    @NoteField(value = "胜利一方")
    private byte winner;

    @NoteField(value = "竞猜时间")
    private long recordTime = System.currentTimeMillis();

    @NoteField(value = "是否已经兑换")
    public byte exchange = 0;

    public static ChampionRaceBetVO of(ChampionRaceSpec spec,
                                       byte winner,
                                       RoleSnapshot snapshotA,
                                       RoleSnapshot snapshotB,
                                       long coins,
                                       long getCoins,
                                       byte side) {
        ChampionRaceBetVO betVO = new ChampionRaceBetVO();
        betVO.setWinner(winner);
        betVO.setSpec(spec);
        betVO.setSnapshotA(snapshotA);
        betVO.setSnapshotB(snapshotB);
        betVO.setCoins(coins);
        betVO.setGetCoins(getCoins);
        betVO.setSide(side);
        return betVO;
    }

    public static ChampionRaceBetVO ofPre(ChampionRaceBetRecord record, long coins, byte side) {
        long getCoins = 0;
        if (side == record.getWinner()) {
            float px = (record.getWinner() == GDFight.Team.A)
                    ? record.getPrePxA()
                    : record.getPrePxB();
            getCoins = (long) Math.floor(px * coins) * GDChampionRace.EXCHANGE_SCALE;
        } else {
            getCoins = new MixRes(GDChampionRace.BET_WRONG_REWARD).items.get(0).num;
        }
        return of(
                record.getSpec(),
                record.getWinner(),
                record.getSnapshotA(),
                record.getSnapshotB(),
                coins,
                getCoins,
                side);
    }

    public byte handleBetRecord(ChampionRaceBetRecord betRecord) {
        if (exchange == 1) {
            return 0;
        }
        exchange = (byte) 1;
        return getSide() == betRecord.getWinner() ? (byte)1 : (byte)2;
    }
}
